<?php

namespace App\Repository\GoodsPhone;

use App\Models\GoodsPhone\GoodsPhoneModel as model;
use Illuminate\Support\Facades\Cache;
use App\Repository\RoleRepository;
use Illuminate\Support\Facades\DB;
use App\Repository\BaseRepository;
use App\Repository\Product\ProductRepository;
use App\Repository\UploadRepository;
use App\Repository\Product\GoodsRemarkRepository;
use App\Repository\Product\GoodsRepository;
use App\Repository\GoodsPhone\GoodsPhoneConfRepository;
use App\Repository\Api\GoodsRepository as apiGoods;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\RotatingFileHandler;
use Monolog\Logger;
use App\Jobs\BigData;
class AutoGoodsPhoneRepository extends  BaseRepository
{
  private $model;
  private $productModel;
  private $log;
  public function __construct()
  {
    $this->model = new model();
    $this->productModel = new ProductRepository();
    parent::__construct();
    $this->log = new Logger('goodsPhoneGet');
    $this->log->pushHandler(
        (new RotatingFileHandler(storage_path('/logs/goodsPhoneGet.log'), 14, Logger::DEBUG, true, 0777))->setFormatter(new LineFormatter(null, null, true, true))
    );
  }
  // 所有的
  public function getPhoneConfig(){
    $where = ['status'=>0];
    $field = ['*'];
    $this->log->info('将获取号码配置加入队列');
    $models =  new GoodsPhoneConfRepository();
    $all = $models->listAll([]);
    $this->log->info('将获取号码配置加入队列'.json_encode($all));
    if($all){
      foreach($all as $key=>$v){
        $goods = DB::table('goods')->where(['id'=>$v->goods_id])->first();
        $this->log->info('将获取号码配置加入队列'.json_encode($goods));
        if(!empty($goods->product_id)){
          // 加入队列 获取号码
          // var_dump($goods->product_id);
          $arrays = [
            'product_id' => $goods->product_id,
            'goods_id' => $v->goods_id,
            'goods_name' => $v->goods_name,
            'goods_code' => $v->goods_code,
            'nums' => $v->numbs,
            'shop_id' => $v->shop_id
          ];
          $this->log->info('将获取号码配置加入队列'.json_encode($arrays));
          // 将某个产品 清空
          $this->model->where(['goods_id'=> $v->goods_id,'shop_id'=>$v->shop_id])->delete();
          dispatch(new BigData($arrays, 'autoGetGoodsPhone',5))->onQueue('BigData'); // 抖音信息验证
        }
      }
    }
    // 每天凌晨运行一次
    $times = date('Y-m-d 01:00:00', strtotime('+1 day'));
    $time = strtotime($times) - time();
    dispatch(new BigData(1, 'autoGetGoodsPhoneConfig',$time))->onQueue('BigData'); // 抖音信息验证
    return false;
  }
  // 自动抓取号码  单个运行
  public function getAutoPhone ($productId,$arr=[]){
    if(empty($arr['nums']) || $arr['nums'] <= 0){
      return $this->returnMsg(100,'运行结束');
    }
    $model = new apiGoods();
    $array = [
      'amounts' => 100
    ];
    $phones = $model->getSelectNumber($productId,$array);
    if(!empty($phones['code']) && $phones['code'] == 200){
      $models = new \App\Repository\GoodsPhone\GoodsPhoneRepository();
      foreach($phones['data'] as $key => $v){
        $v= json_decode(json_encode($v),true);
        $arrays = [];
        $arrays['phone'] = $v['iccid'];
        $arrays['provite'] = $v['province'];
        $arrays['city'] = $v['city'];
        if(!empty($v['chtype'])){
          $arrays['chtype'] = $v['chtype'];
        }
        $shop_id = !empty($arr['shop_id'])?$arr['shop_id']:'';
        $arrays['goods_id'] = !empty($arr['goods_id'])?$arr['goods_id']:'';
        $arrays['goods_name'] = !empty($arr['goods_name'])?$arr['goods_name']:'';
        $arrays['goods_code'] = !empty($arr['goods_code'])?$arr['goods_code']:'';
        $models ->add($arrays,$shop_id);
      }
    }
    $arr['nums'] = $arr['nums'] -1;
    dispatch(new BigData($arr, 'autoGetGoodsPhone',20))->onQueue('BigData'); // 抖音信息验证
    return $this->returnMsg(200,'部分运行完成',$arr);
  }
}
